﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hazop
{
    public partial class ProgressForm : Form
    {
        public ProgressForm()
        {
            InitializeComponent();
        }

        public void ShowMessage(string msg,bool canCancel){
            if(InvokeRequired){
                Invoke(new Action(()=> {
                                      _lbMessage.Text = msg;
                                      _btnCancel.Visible = canCancel;
                                  }
                    ));
            }
            else{
                _lbMessage.Text = msg;
                _btnCancel.Visible = canCancel;
            }
        }

        public void CloseForm(){
            if(InvokeRequired){
                Invoke(new Action(Close));
            }
            else{
                Close();
            }
        }

        public void ShowError(Exception exception){
            if(InvokeRequired){
                Invoke(new Action<Exception>(DisplayError),exception);
            }
            else{
                DisplayError(exception);
            }
        }

        private void  DisplayError(Exception e){
            _lbMessage.Text = "Action runs in error! " + e.Message;
            _lbMessage.ForeColor = Color.Red;
            _btnOK.Visible = true;
            _btnCancel.Visible = false;
        }

        private void _btnCancel_Click(object sender, EventArgs e)
        {
            if(CancelClick != null){
                CancelClick();
            }
        }

        public event Action CancelClick;

    }
}
